/*
 * @Description:
 * @Version: 2.0
 * @Autor: zhangjx
 * @Date: 2023-05-25 14:19:02
 * @LastEditors: uphldjx 1373577124@qq.com
 * @LastEditTime: 2024-01-08 14:30:45
 */
import { defineStore } from 'pinia'
import apiMod from '@/apiMod/index'
import { getStore } from '@/utils/store'
import MQTT from '@/utils/mqtt'

export const useCommonStore = defineStore('common', () => {
	// 用于老人减免申请的申请时间比较，判断要不要清除表格中的名单数据
	let applyDateComp:any = ref('')
	// 为了服务组织用户等页面的左边的数据树只刷新一次
	let treeRefreshData: any = ref({
		// 服务组织用户
		FWZZ_user: false,
		// 大众用户
		DZ_user: false,
		// 政务用户
		ZW_user: false,
		// 行政区划
		XZ_user: false,
	})
	// 服务对象等列表中的数据不能满足详情的表头时，保存详情中数据来展示到详情中
	let detailHeadData:any = ref(undefined)
	let blockChainHeight:any = ref(0)
	let gloabnowRowData: any = ref({})
	let searchMode = ref()
	const isCollapse = ref(false)
	const FullScreen = ref(false)
	let dictList = ref({})
	const queryObj:any = ref({})
	const routerTransition = ref('fade-slide')
	let chiMenu = ref({})
	const tempNowObj: any = ref({})
	const PublicMqtt = ref<MQTT | null>(null)

	
	const moreSearchInfo = ref({
		type: '',
		params: {},
		model: {},
		otherModel: {},
	})

	const setApplyDateComp = (data:any) =>{
		applyDateComp.value = data || ''
	}
	const setDetailHeadData = (data:any)=>{
		detailHeadData.value = data||{}
	}
	const setBlockChainHeight = (data:any) =>{
		blockChainHeight.value = data
	}
	const setTreeRefreshData = (data: any) => {
		if (data == 4) {
			// 服务组织用户
			treeRefreshData.value.FWZZ_user = true
		} else if (data == 1) {
			// 大众用户
			treeRefreshData.value.DZ_user = true
		} else if (data == 3) {
			// 政务用户
			treeRefreshData.value.ZW_user = true
		} else if (data == 2) {
			treeRefreshData.value.XZ_user = true
		}
		console.log('pandaun', treeRefreshData.value)
	}
	const setGloabnowRowData = (data: any) => {
		gloabnowRowData.value = data
	}
	// 设置搜索模式，用于区分全字段搜索还是反向查询
	const setSearchMode = (mode: string | null | undefined) => {
		searchMode.value = mode
	}
	const setCollapse = (show: boolean) => {
		isCollapse.value = !isCollapse.value
		document.documentElement.style.setProperty('--vt-nav-aside-width', isCollapse.value ? '65px' : '240px')
	}
	const Loading = ref(false)
	const LoadingTitle = ref('内容加载中')
	const setFullScreen = (fullClassName?: string) => {
		let fullarea = fullClassName ? document.getElementsByClassName(fullClassName)[0] : document.documentElement
		if (!document.fullscreenElement) {
			// 全屏
			fullarea.requestFullscreen()
			FullScreen.value = true
		} else {
			// 退出全屏
			if (document.exitFullscreen) {
				document.exitFullscreen()
				FullScreen.value = false
			}
		}
	}

	// 根据codes 获取数据字典集合
	const getDictList = async (code) => {
		try {
			if (code && code.length > 0) {
				const { data } = await apiMod.getDictItemList(code)
				dictList.value = data
			}
		} catch (error) {}
	}
	// 设置全局参数变量
	const setGloablParams = (query) => {
		queryObj.value = query || {}
	}
	//设置全局的临时变量参数
	const setTempParams = (query) => {
		tempNowObj.value = query
	}
	//设置批量查询条件
	const setMoreSearch = (query) => {
		moreSearchInfo.value = query
	}
	// 设置系统主题
	const setBaseColor = () => {
		const themeConfig = getStore('themeConfig') || {}
		let object = themeConfig.config || {}
		for (const key in object) {
			if (Object.prototype.hasOwnProperty.call(object, key)) {
				const element = object[key]
				document.documentElement.style.setProperty(key, element)
			}
		}
	}
	const setLoading = (status?: boolean, title?: string) => {
		Loading.value = !Loading.value
		LoadingTitle.value = title
	}
	// 初始化链接mqtt
	const setInitMqtt = () => {
		PublicMqtt.value = new MQTT()
		PublicMqtt.value.init()
	}
	return {
		blockChainHeight,
		detailHeadData,
		PublicMqtt,
		Loading,
		LoadingTitle,
		dictList: dictList,
		tempNowObj,
		moreSearchInfo,
		chiMenu,
		FullScreen,
		searchMode,
		isCollapse,
		routerTransition,
		queryObj,
		gloabnowRowData,
		treeRefreshData,
		applyDateComp,
		setDetailHeadData,
		setBlockChainHeight,
		setTempParams,
		setMoreSearch,
		setCollapse,
		setSearchMode,
		setFullScreen,
		getDictList,
		setGloablParams,
		setBaseColor,
		setLoading,
		setGloabnowRowData,
		setTreeRefreshData,
		setInitMqtt,
		setApplyDateComp,
	}
})

export const useConfigStore = defineStore('config', () => {
	const selection = ref(false)

	const setTableSelection = (show?: boolean) => {
		if (show == undefined) {
			selection.value = !selection.value
		} else {
			selection.value = show
		}
	}

	return {
		selection,
		setTableSelection,
	}
})
